var registerDAO = require('../models/registerDAO')
var formidable = require('formidable')
var path = require('path')
var jwt = require('jsonwebtoken')
var bcrypt = require('bcrypt')
var registercontroller = {
    register: function (req, res) {
        var user = { userTel: req.body.telephone, userPwd: req.body.userPassword }
        console.log(user)
        if(user.userTel.length != 0){
            console.log(user.userTel)
            if (user.userTel.length == 11 ) {
                registerDAO.getUserByTel(user.userTel, function (err, results) {
                    // console.log(results)
                            if (results.length == 0) {
                                if(user.userPwd.length != 0){
                                    // console.log(user.userPwd.length)
                                    bcrypt.genSalt(10, function (err, salt) {
                                        bcrypt.hash(user.userPwd, salt, function (err, hash) {
                                            user.userPwd = hash
                                            registerDAO.register(user, function (err, results) {
                                                if (err) {
                                                    res.status(500).json({code: 500, data:results, msg: '数据库错误，注册失败!' })
                                                } else {
                                                    res.status(200).json({code: 200, data:results, msg: '1' })
                                                }
                                            })
                                        });
                                    });
                                }else{
                                    res.status(500).json({ code: 500, data: [], msg: '密码不能为空' })
                                }     
                        } else {
                            res.status(500).json({code: 500, data:[], msg: '手机号已存在,不可重复注册' })
                        }
            }); 
             }else{
                        res.status(500).json({code: 500, data:[], msg: '手机号非法' })
                    }
        }else{
            res.status(500).json({code: 500, data:[], msg: '手机号不能为空' })
        }
   
    },
login: function(req, res) {
    var user = { userTel: req.body.telephone, userPwd: req.body.userPassword }
    registerDAO.getUserByTel(user.userTel, function (err, results) {
        if (err) {
            res.status(500).json({code: 500, data:results, msg: '数据库错误，登录失败' })
        } else {
            if(user.userTel.length !=0){
                if (results == null || results.length != 1) {
                    res.status(200).json({code: 500, data:results, msg: '手机号不存在，登录失败!' })
                } else {
                    if(user.userPwd.length != 0){
                        bcrypt.compare(user.userPwd, results[0].userPassword, function (err, resPwd) {
                            console.log(user.userPwd)
                            console.log(results[0].userPassword)
                            console.log(resPwd)
                            if (resPwd) {
                                
                                jwt.sign({ userTel: user.userTel ,userId:results[0].userId,role:results[0].role}, 'privateKey', { expiresIn: 60 * 60}, function (err, token) {
                                    console.log('token',token);
                                    res.status(200).json({code: 200, data:results, msg: '登录成功！！', token: 'Bearer ' + token })
                                });
                            } else {
                                res.json({code: 500, data:results, msg: '密码错误，登录失败！！' })
                            }
                        })
                    }else{
                        res.status(500).json({code: 500, data:results,msg: '请输入密码！' })
                    }
                    
                }
            }else{
                res.status(500).json({code: 500, data:results, msg: '请输入手机号!' })
            }
            
        }
    })
},
forpwd: function(req, res) {
    var user = { userTel: req.body.telephone, userPwd: req.body.userPassword }
    var userTel = req.body.telephone
    console.log(user)
    registerDAO.getUserByTel(userTel, function (err, results) {
            if ( results == null || results.length != 1) {
                console.log( results.length == 1)
                res.status(500).json({code: 500, data:results,msg: '手机号不存在，请重新输入!' })
            } else {
                        bcrypt.genSalt(10, function (err, salt) {
                            bcrypt.hash(user.userPwd, salt, function (err, hash) {
                                user.userPwd = hash
                                registerDAO.forgetpwd(user, function (err, results) {
                                    if (err) {
                                        res.status(500).json({code: 500, data:results, msg: '0!' })
                                    } else {
                                        res.status(200).json({code: 200, data:results, msg: '1!' })
                                    }
                                })
                        });
                  }); 
            }
    })
},
}
module.exports = registercontroller